package com.wz.leetcode.string;

public class LongestSubstring_3 {
    public static int lengthOfLongestSubstring(String s){
        int i, j = 0, count = 0, max = 0;
        int[] index = new int[128];
        int start = 0;
        for(i=0; i < s.length(); i++) {
            if(index[s.charAt(i)] >= start){
                //index用来储存出现重复字符时
                //子串起始下标应移动到的地方
                count = i-start;
                if (count > max) {
                    max = count;
                }
                start = index[s.charAt(i)];
            }
            index[s.charAt(i)] = i+1; //如果 <
        }
        count = i-start;
        return count > max ? count : max;
    }

    public static void main(String[] args) {
        System.out.println("abcabcbb longest substr: " + lengthOfLongestSubstring("abcabcbb"));
        System.out.println("bbbbb longest substr: " + lengthOfLongestSubstring("bbbbb"));
        System.out.println("pwwkew longest substr: " + lengthOfLongestSubstring("pwwkew"));
        System.out.println("abcd longest substr: " + lengthOfLongestSubstring("abcd"));
    }
}
